/**
 * @description 复制、黏贴模板
 * 1. 整个页面的复制、黏贴
 * 2. 功能块的复制、黏贴
 */
import { message } from "antd";
import { replaceAllStyle } from "./style";

const localKey = 'template_copy'
// 过期 30分钟过期
const expireTime = 10 * 60 * 1000
/**
 * @description 复制模版
 * @param { string } moduleKey 模块名
 * @param { Array } paramKeys 键字段数组
 * @param { object } form 表单实例 
 */
export async function copyTemplate(moduleKey, paramKeys, form) {
  try {
    let textData = {
      type: 'module',
      timestamp: Date.now(),
      moduleKey,
      paramKeys,
      formData: form.getFieldValue(paramKeys)
    }
    textData = JSON.stringify(textData)
    localStorage.setItem(localKey, textData)

    message.open({
      type: 'success',
      content: '复制成功'
    })

  } catch (error) {

    message.open({
      type: 'error',
      content: `复制失败:${error.message}`
    })

  }

}
export async function pasteTemplate(form, onConfigEvent, currentModuleKey) {
  try {
    //[0] 解析 剪贴板数据
    let text = localStorage.getItem(localKey)

    if (!text) {
      message.open({
        type: 'error',
        content: '请先复制'
      })
      return
    }
    let data = JSON.parse(text)

    // [1] 检测是否过期
    if (Date.now() - data.timestamp > expireTime) {
      localStorage.removeItem(localKey)
      message.open({
        type: 'error',
        content: '请先复制'
      })
      return
    }


    let { type, formData, paramKeys, moduleKey } = data

    if (type !== 'module') {
      message.open({
        type: 'error',
        content: '你复制的是模块，请到对应的【模块】里黏贴'
      })

      return
    }

    if (currentModuleKey !== moduleKey) {

      message.open({
        type: 'error',
        content: '请到对应的【页面】里黏贴'
      })

      return
    }

    // [1] 更新表单数据
    form.setFieldValue(paramKeys, formData)

    // [2] 更新模板数据
    onConfigEvent(paramKeys, formData)

    // [3] 更新style文件
    replaceAllStyle(currentModuleKey, formData)

    message.open({
      type: 'success',
      content: '黏贴成功'
    })
  } catch (error) {

    message.open({
      type: 'error',
      content: `黏贴失败:${error.message}`
    })
  }

}

/**
 * @description 复制模块
 * @param { string } moduleKey 模块名字。比如: 首页home
 * @param { string } blockKey 功能块名字。比如: 分类category
 * @param { string } paramKeys 表单的属性keys。 比如: ['category']
 * @param { string } classKey 前缀key 比如: listpage
 * @param { object } form 表单form实例
 */
export async function copyBlock(moduleKey, blockKey, paramKeys, classKey, form) {

  try {
    let textData = {
      type: 'block',
      timestamp: Date.now(),
      moduleKey,
      paramKeys,
      blockKey,
      classKey,
      blockData: form.getFieldValue(paramKeys)
    }
    textData = JSON.stringify(textData)
    localStorage.setItem(localKey, textData)

    message.open({
      type: 'success',
      content: '复制成功'
    })

  } catch (error) {

    message.open({
      type: 'error',
      content: `复制失败:${error.message}`
    })

  }

}

/**
 * 黏贴模块
 * @param {*} form 表单form实例
 * @param {*} onConfigEvent 
 * @param {*} currentModuleKey 模块名字。比如: 首页home
 * @param {*} currentBlockKey 功能块名字。比如: 分类category
 * @param {*} currentParamKeys 表单的属性keys。 比如: ['category']
 * @param {*} currentClassKey 前缀key 比如: listpage
 * @returns 
 */
export async function pasteBlock(currentModuleKey, currentBlockKey, currentParamKeys, currentClassKey, form, onConfigEvent) {
  try {
    //[0] 解析 剪贴板数据
    let text = localStorage.getItem(localKey)

    if (!text) {
      message.open({
        type: 'error',
        content: '请先复制'
      })
      return
    }
    let data = JSON.parse(text)

    // [1] 检测是否过期
    if (Date.now() - data.timestamp > expireTime) {
      localStorage.removeItem(localKey)
      message.open({
        type: 'error',
        content: '请先复制'
      })
      return
    }


    let { type, blockData, moduleKey, blockKey, paramKeys, classKey } = data

    if (type !== 'block') {
      message.open({
        type: 'error',
        content: '你复制的是页面，请到对应的【页面】里黏贴'
      })

      return
    }

    if (currentBlockKey !== blockKey) {

      message.open({
        type: 'error',
        content: '请到对应的【模块】里黏贴'
      })

      return
    }

    // [0] 清洗数据
    for (const key in blockData) {

      let classname = blockData[key].classname

      // ps：只有样式的对象才处理。其它属性不动
      if (classname && classKey !== currentClassKey) {
        
        // 只匹配第一个listpage
        // classname: ".listpage .category, .listpage .test" -> ".news .category, .ews .test"
        let classnames = classname.split(',')
        let newClassnames = classnames.map(item => item.replace(classKey, currentClassKey))
        let newClassname = newClassnames.join(',')

        // key: listpage_box -> news__box
        let newKey = key.replace(classKey, currentClassKey)

        // 修改key 和 classname
        blockData[newKey] = blockData[key]
        blockData[newKey].classname = newClassname

        // 把旧的删除掉
        delete blockData[key]
      }
    }



    // [1] 更新表单数据
    form.setFieldValue(currentParamKeys, blockData)

    // [2] 更新模板数据
    onConfigEvent(currentParamKeys, blockData)

    // [3] 更新style文件
    let formData = form.getFieldsValue(true)
    replaceAllStyle(currentModuleKey, formData)

    message.open({
      type: 'success',
      content: '黏贴成功'
    })
  } catch (error) {

    message.open({
      type: 'error',
      content: `黏贴失败:${error.message}`
    })
  }
}


/**
 * @description 2026年-黏贴模板
 * @param {*} form 
 * @param {*} onConfigEvent 
 * @param {*} currentModuleKey 
 * @returns 
 */
export async function pasteTemplate_2026(form, onConfigEvent, currentModuleKey) {
  try {
    //[0] 解析 剪贴板数据
    let text = localStorage.getItem(localKey)

    if (!text) {
      message.open({
        type: 'error',
        content: '请先复制'
      })
      return
    }
    let data = JSON.parse(text)

    // [1] 检测是否过期
    if (Date.now() - data.timestamp > expireTime) {
      localStorage.removeItem(localKey)
      message.open({
        type: 'error',
        content: '请先复制'
      })
      return
    }


    let { type, formData, paramKeys, moduleKey } = data

    if (type !== 'module') {
      message.open({
        type: 'error',
        content: '你复制的是模块，请到对应的【模块】里黏贴'
      })

      return
    }


    // [1] 更新表单数据
    form.setFieldValue(paramKeys, formData)

    // [2] 更新模板数据
    onConfigEvent(paramKeys, formData)


    message.open({
      type: 'success',
      content: '黏贴成功'
    })
  } catch (error) {

    message.open({
      type: 'error',
      content: `黏贴失败:${error.message}`
    })
  }

}